import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
import { setMeshImageToJSON } from "./parseModel";

self.addEventListener("message", (e) => {
    const { url } = e.data;
    if (!url) return;

    const loader = new GLTFLoader();
    const dracoLoader = new DRACOLoader();
    dracoLoader.setDecoderPath("/static/wasms/");
    dracoLoader.setDecoderConfig({ type: "js" });
    loader.setDRACOLoader(dracoLoader);

    loader.load(
        url,
        async (gltf) => {
            let json = await setMeshImageToJSON(gltf.scene);
            console.log(new Date())
            self.postMessage({
                type: "loaded",
                model: json,
            });
        },
        (xhr) => {
            const percent = xhr.loaded / xhr.total;
            let percentage = Math.floor(percent * 100);
            // console.log(percentage)
            // self.postMessage({
            //     type: "loading",
            //     percentage,
            // });
        }
    );
});
